//
//  GrowViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/7/26.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "GrowViewController.h"
#import "ShaiXuanViewController.h"
#import "GrowTopTableViewCell.h"
#import "GrowContentTableViewCell.h"
#import "OrderViewController.h"
#import "AddChengzhangViewController.h"
#import "GrowDetailViewController.h"

@interface GrowViewController ()<UITableViewDelegate,UITableViewDataSource,OrderSelectDelegate,deleteDelegates>
{
    UIButton * navSelection1;
    UIButton * navSelection2;
    
    UITableView * contentTableView;
    NSArray * dataArr;
    
    NSString * vtype;
    NSString * vproperty;
    NSString * vorderbase;
    NSString * vorderrule;
    NSString * vpage;
    NSString * vtime;
    NSString * vnexttime;
    NSString * vshowtype;
    NSString * vuid;
    
    UIButton * black;
    UIView * select;
}
@end

@implementation GrowViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationItem.title = @"见证成长";
    
    vtype = @"6";
    vproperty = @"4";
    vorderbase = @"0";
    vorderrule = @"0";
    vpage = @"1";
    vtime = @"";
    vnexttime = @"";
    vshowtype = @"3";
    vuid = @"";
    
    UIBarButtonItem * left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ios-settings"]  style:UIBarButtonItemStylePlain target:self action:@selector(shaixuan)];
    self.navigationItem.leftBarButtonItem = left;
    
    
    UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"发布"] style:UIBarButtonItemStylePlain target:self action:@selector(addGrow)];
    self.navigationItem.rightBarButtonItem = right;
    
    
    UIView * titleView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, ScreenWidth - 170, 40)];
    
    navSelection1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, titleView.frame.size.width/2, 40)];
    [navSelection1 setTitle:@"见证成长" forState:UIControlStateNormal];
    navSelection1.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [navSelection1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [navSelection1 setTitleColor:themeColor forState:UIControlStateSelected];
    [navSelection1 addTarget:self action:@selector(selection1) forControlEvents:UIControlEventTouchUpInside];
    
    UIView * lineDown = [[UIView alloc]initWithFrame:CGRectMake(titleView.frame.size.width/2, 10, 1, 20)];
    lineDown.backgroundColor = [UIColor blackColor];
    [titleView addSubview:lineDown];
    
    navSelection2 = [[UIButton alloc]initWithFrame:CGRectMake(titleView.frame.size.width/2, 0, titleView.frame.size.width/2, 40)];
    [navSelection2 setTitle:@"相册" forState:UIControlStateNormal];
    navSelection2.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [navSelection2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [navSelection2 setTitleColor:themeColor forState:UIControlStateSelected];
    [navSelection2 addTarget:self action:@selector(selection2) forControlEvents:UIControlEventTouchUpInside];
    
    [titleView addSubview:navSelection1];
    [titleView addSubview:navSelection2];
    self.navigationItem.titleView = titleView;
    
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, rectNav.size.height + rectStatus.size.height, ScreenWidth, ScreenHeight - 44 - rectNav.size.height - rectStatus.size.height) style:UITableViewStylePlain];
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [self.view addSubview:contentTableView];
    
    [self selection1];
}

-(void)selection1
{
    navSelection1.selected = YES;
    navSelection2.selected = NO;
//    UIBarButtonItem * left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ios-settings"]  style:UIBarButtonItemStylePlain target:self action:@selector(shaixuan)];
//    self.navigationItem.leftBarButtonItem = left;
//
//    UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"发布"] style:UIBarButtonItemStylePlain target:self action:@selector(addGrow)];
//    self.navigationItem.rightBarButtonItem = right;
    [self requestData];

}

-(void)selection2
{
    navSelection1.selected = NO;
    navSelection2.selected = YES;
//    self.navigationItem.leftBarButtonItem = nil;
//    self.navigationItem.rightBarButtonItem = nil;
    [self requestData];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self requestData];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

-(void)requestData
{
    if (navSelection1.selected)
    {
        [NetRequestCenter loadpersonalarchiveWityType:vtype property:vproperty ordrebase:vorderbase orderrule:vorderrule page:vpage time:vtime nexttime:vnexttime showtype:vshowtype uid:vuid Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
    else
    {
        [NetRequestCenter loadpersonalarchiveWityType:@"7" property:@"4" ordrebase:@"0" orderrule:@"0" page:@"1" time:vtime nexttime:@"" showtype:@"0" uid:@"" Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
}

-(void)shaixuan
{
    if (black)
    {
        [self cancleSelection];
    }

    OrderViewController * order = [[OrderViewController alloc]init];
    order.type = vtype;
    order.property = vproperty;
    order.orderbase = vorderbase;
    order.orderrule = vorderrule;
    order.page = vpage;
    order.time = vtime;
    order.nexttime = vnexttime;
    order.showtype = vshowtype;
    order.uid = vuid;
    order.delegate = self;
    [self.navigationController pushViewController:order animated:YES];
    self.tabBarController.tabBar.hidden = YES;
}

-(void)orderResultWithType:(NSString *)type property:(NSString *)property orderbase:(NSString *)orderbase orderrult:(NSString *)orderrult page:(NSString *)page time:(NSString *)time nexttime:(NSString *)nexttime showtype:(NSString *)showtype uid:(NSString *)uid
{
    vtype = type;
    vproperty = property;
    vorderbase = orderbase;
    vorderrule = orderrult;
    vpage = page;
    vtime = time;
    vnexttime = nexttime;
    vshowtype = showtype;
    vuid = uid;
    [self requestData];
}

-(void)addGrow
{
    if (black)
    {
        [self cancleSelection];
    }
    black = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    black.backgroundColor = [UIColor blackColor];
    black.alpha = 0.3;
    [black addTarget:select action:@selector(cancleSelection) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:black];
    
    select = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - ScreenWidth/3 * 2, ScreenWidth, ScreenWidth/3 * 2)];
    select.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:select];
    
    UIButton * button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3, ScreenWidth/3)];
    [button1 addTarget:self action:@selector(addSelection:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 1;
    [select addSubview:button1];

    UIButton * button2 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3, 0, ScreenWidth/3, ScreenWidth/3)];
    [button2 addTarget:self action:@selector(addSelection:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 2;
    [select addSubview:button2];
    
    UIButton * button3 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3 * 2, 0, ScreenWidth/3, ScreenWidth/3)];
    [button3 addTarget:self action:@selector(addSelection:) forControlEvents:UIControlEventTouchUpInside];
    button3.tag = 3;
    [select addSubview:button3];
    
    UIButton * button4 = [[UIButton alloc]initWithFrame:CGRectMake(0, ScreenWidth/3, ScreenWidth/3, ScreenWidth/3)];
    [button4 addTarget:self action:@selector(addSelection:) forControlEvents:UIControlEventTouchUpInside];
    button4.tag = 4;
    [select addSubview:button4];
    
    UIButton * button5 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3, ScreenWidth/3, ScreenWidth/3, ScreenWidth/3)];
    [button5 addTarget:self action:@selector(addSelection:) forControlEvents:UIControlEventTouchUpInside];
    button5.tag = 5;
    [select addSubview:button5];
    
    UIButton * button6 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3 * 2, ScreenWidth/3, ScreenWidth/3, ScreenWidth/3)];
    [button6 addTarget:self action:@selector(addSelection:) forControlEvents:UIControlEventTouchUpInside];
    button6.tag = 6;
    [select addSubview:button6];
    
    {
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        image.image = [UIImage imageNamed:@"个人大事记"];
        image.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 - 10);
        [button1 addSubview:image];
        
        UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3, 20)];
        title.font = [UIFont systemFontOfSize:15];
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        title.text = @"个人大事记";
        title.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 + 30);
        [button1 addSubview:title];
    }
    {
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        image.image = [UIImage imageNamed:@"个人采风"];
        image.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 - 10);
        [button2 addSubview:image];
        
        UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3, 20)];
        title.font = [UIFont systemFontOfSize:15];
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        title.text = @"个人采风";
        title.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 + 30);
        [button2 addSubview:title];
    }
    {
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        image.image = [UIImage imageNamed:@"校园活动"];
        image.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 - 10);
        [button3 addSubview:image];
        
        UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3, 20)];
        title.font = [UIFont systemFontOfSize:15];
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        title.text = @"校园活动";
        title.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 + 30);
        [button3 addSubview:title];
    }
    {
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        image.image = [UIImage imageNamed:@"社会活动"];
        image.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 - 10);
        [button4 addSubview:image];
        
        UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3, 20)];
        title.font = [UIFont systemFontOfSize:15];
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        title.text = @"社会活动";
        title.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 + 30);
        [button4 addSubview:title];
    }
    {
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        image.image = [UIImage imageNamed:@"技能资质"];
        image.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 - 10);
        [button5 addSubview:image];
        
        UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3, 20)];
        title.font = [UIFont systemFontOfSize:15];
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        title.text = @"技能资质";
        title.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 + 30);
        [button5 addSubview:title];
    }
    {
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        image.image = [UIImage imageNamed:@"梦想舞台"];
        image.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 - 10);
        [button6 addSubview:image];
        
        UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3, 20)];
        title.font = [UIFont systemFontOfSize:15];
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        title.text = @"梦想舞台";
        title.center = CGPointMake(ScreenWidth/6, ScreenWidth/6 + 30);
        [button6 addSubview:title];
    }
    
    self.tabBarController.tabBar.hidden = YES;
}

-(void)addSelection:(UIButton *)button
{
    [black removeFromSuperview];
    [select removeFromSuperview];
    
    AddChengzhangViewController * add = [[AddChengzhangViewController alloc]init];
    add.type = button.tag;
    [self.navigationController pushViewController:add animated:YES];
}

-(void)cancleSelection
{
    [black removeFromSuperview];
    [select removeFromSuperview];
    self.tabBarController.tabBar.hidden = NO;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArr count] + 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return [GrowTopTableViewCell height];
    }
    else
    {
        return [GrowContentTableViewCell heightwithData:[dataArr objectAtIndex:indexPath.row - 1]];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        static NSString * identifer = @"identifer";
        GrowTopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[GrowTopTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        return cell;
    }
    else
    {
        static NSString * identifer = @"sdsd";
        GrowContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[GrowContentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.delegate = self;
        [cell loadData:[dataArr objectAtIndex:indexPath.row - 1]];
        return cell;
    }
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GrowDetailViewController * cotnent = [[GrowDetailViewController alloc]init];
    cotnent.contentId = [[dataArr objectAtIndex:indexPath.row - 1]objectForKey:@"ArchivesUID"];
    [self.navigationController pushViewController:cotnent animated:YES];
    
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

-(void)deleteContent:(NSDictionary *)content
{
    [SVProgressHUD show];
    [NetRequestCenter personalarchiveWithid:[content objectForKey:@"ArchivesUID"] result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            [SVProgressHUD showSuccessWithStatus:@"成功"];
            [self requestData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

-(void)editContent:(NSDictionary *)content
{
    NSString * cotnentId = [content objectForKey:@"ArchivesUID"];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * item1 = [UIAlertAction actionWithTitle:@"全部可见" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [NetRequestCenter editarchiveshowtypeWithid:cotnentId showtype:@"0" result:^(BOOL success, NSDictionary *resultDic) {
            if (success)
            {
                [self requestData];
            }
        }];
    }];
    UIAlertAction * item2 = [UIAlertAction actionWithTitle:@"仅自己" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [NetRequestCenter editarchiveshowtypeWithid:cotnentId showtype:@"1" result:^(BOOL success, NSDictionary *resultDic) {
            if (success)
            {
                [self requestData];
            }
        }];
    }];
    UIAlertAction * item3 = [UIAlertAction actionWithTitle:@"好友" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [NetRequestCenter editarchiveshowtypeWithid:cotnentId showtype:@"2" result:^(BOOL success, NSDictionary *resultDic) {
            if (success)
            {
                [self requestData];
            }
        }];
    }];
 
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:item1];
    [alertController addAction:item2];
    [alertController addAction:item3];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)showImages:(NSArray *)images
{
    MYPhotoBrowser *photoBrowser = [[MYPhotoBrowser alloc]initWithUrls:images imgViews:nil placeholder:nil currentIdx:index handleNames:nil callback:^(UIImage *handleImage,NSString *handleType) {
        
    }];
    [photoBrowser showWithAnimation:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
